#include "CSerialPort.h"
#include "Tracker.h"
#include <opencv2/videoio.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <iomanip>
#include <termios.h>
#include <netinet/in.h>
#include<sys/ioctl.h>
#include <thread>
using namespace std;
using namespace cv;

const int IMG_WIDTH = 2688, IMG_HEIGHT = 1520;

vector<Point2f> center;
CSerialPort myserialport; //打开串口
char send_data[8];

int main(int argc, char **argv)
{
	CommandLineParser parser(argc, argv, "{@input_path |0|input path can be a camera id, like 0,1,2 or a video filename}");
	parser.printMessage();
	string input_path = parser.get<string>(0);
	string video_name = input_path;
	VideoCapture video_in;
	video_in.set(CAP_PROP_FRAME_WIDTH, 320);
	video_in.set(CAP_PROP_FRAME_HEIGHT, 240);
	if ((isdigit(input_path[0]) && input_path.size() == 1))
	{
		int camera_no = input_path[0] - '0';
		video_in.open(camera_no);
	}
	else
	{
		video_in.open(video_name);
	}
	if (!video_in.isOpened())
	{
		cerr << "Couldn't open " << video_name << endl;
		return 1;
	}
	time_t rawtime;
	struct tm *ptm;
	Size videoSize = Size((int) video_in.get(CAP_PROP_FRAME_WIDTH),    // Acquire input size
                  (int) video_in.get(CAP_PROP_FRAME_HEIGHT));	
	time(&rawtime);
    ptm = gmtime(&rawtime);
	string title=to_string(ptm->tm_yday)+"-"+to_string(ptm->tm_hour)+"-"+to_string(ptm->tm_min)+"-"+to_string(ptm->tm_sec);

    title = "/home/ubuntu/桌面/golfbos/source/" + title + ".avi";
    VideoWriter writer(title, CV_FOURCC('M', 'J', 'P', 'G'), video_in.get(CAP_PROP_FPS), videoSize);
	int  iFrameHeight = video_in.get(CV_CAP_PROP_FRAME_HEIGHT);  
        int  iFrameWidth  = video_in.get(CV_CAP_PROP_FRAME_WIDTH);  
	
	Mat frame;
	namedWindow(video_name);

// 	cout << "\nPress any key to stop the video and select a bounding box" << endl;
	// cv::Rect uBox;
 	// uBox=Rect(213,57,304,303);
	while (waitKey(20) < 1)
	{
		video_in >> frame;
		imshow(video_name, frame);
		writer<<frame;

		if (waitKey(1)<1)
{
	time(&rawtime);
    ptm = gmtime(&rawtime);
	title=to_string(ptm->tm_yday)+"-"+to_string(ptm->tm_hour)+"-"+to_string(ptm->tm_min)+"-"+to_string(ptm->tm_sec);

    title = "/home/ubuntu/桌面/golfbos/source/" + title + ".jpg";
}
		//	cout<<iFrameHeight<<"   "<<iFrameWidth<<endl;

	}
	//vector<Point2f> bb;

	return 0;
}
